package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author: yp
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 根据用户名查询
     *
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        //取出userId, 查询出角色
        if(user != null){
            Set<Role> roles = roleDao.findByUid(user.getId());
            //遍历
            if(roles != null && roles.size()>0){
                for (Role role : roles) {
                    //取出roleid, 查询权限
                    Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
                    role.setPermissions(permissions);
                }
            }

            user.setRoles(roles);

        }

        return user;
    }

//    分页查询:
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.调用分页插件的方法
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2.调用Dao, 封装PageResult
        Page<User> page =  userDao.findConditions( queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    //添加用户

    @Override
    public void add(User user, Integer[] checkitemIds) {
        //1.调用Dao,保存CheckGroup【返回当前保存的id】
        //这里密码先加密再保存
        String password = user.getPassword();
        user.setPassword(bCryptPasswordEncoder.encode(password));
        userDao.add(user);
        //2.调用Dao, 把选择的检查项和检查组的id保存到t_checkgroup_checkitem
        Integer userId = user.getId();
        setCheckGroupAndCheckItem(userId,checkitemIds);
    }

    //编辑回显
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    //编辑回显角色
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        return userDao.findCheckItemIdsByCheckGroupId(checkGroupId);
    }

    //更新
    @Override
    public void edit(User checkGroup, Integer[] checkitemIds) {
        //1.调用dao 更新CheckGroup(t_checkgroup)
        //先把密码加密再更新
        String password = checkGroup.getPassword();
        checkGroup.setPassword(bCryptPasswordEncoder.encode(password));
        userDao.edit(checkGroup);
        //2.调用dao 先把CheckGroup关联的检查项给删除(t_checkgroup_checkitem)
        userDao.deleteAssociation(checkGroup.getId());
        //3.调用dao 把更新提交过来的checkitemIds 再进行关联
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);

    }

    @Override
    public void delete(Integer checkGroupId) {
        //        删除中间表数据
        userDao.delet(checkGroupId);
//        删除检查组数据:
        userDao.deleteCheckGroupId(checkGroupId);
    }

    //向t_user_role保存数据
    private void setCheckGroupAndCheckItem(Integer userId, Integer[] checkitemIds) {
        if(checkitemIds != null){
            for (Integer checkItemId : checkitemIds) {
                Map map = new HashMap();
                map.put("userId",userId);
                map.put("roleId",checkItemId);
                userDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
